package com.example.myyiyanmengjava;

import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.example.mvplibrary.ui.activity.BaseActivity;
import com.example.myyiyanmengjava.adapter.FragTabAdapter;
import com.example.myyiyanmengjava.bean.TiTypeListBean;
import com.example.myyiyanmengjava.fragment.VpFragment;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.ResourceObserver;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity<T> extends BaseActivity {
    private String TAG = "朱勇闯Main";

    private static List<TiTypeListBean.InfoBean.UTypeBean> tiTypeList;
    private List<String> tiTypeListString;
    private ArrayList<Fragment> fragments;
    private TabLayout mTabMain;
    private ViewPager mVpNavMain;
    private FragTabAdapter fragTabAdapter;


    @Override
    protected void initData() {
        initView();

    }

    private void initTiTypeList() {
        new Retrofit.Builder()
                .baseUrl(ApiService.baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build()
                .create(ApiService.class)
                .getTiTypeList("index.php/Shiti/ti_type_list")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ResourceObserver<TiTypeListBean>() {
                    @Override
                    public void onNext(TiTypeListBean tiTypeListBean) {
                        tiTypeList = tiTypeListBean.getInfo().getU_type();
//                        Log.e(TAG, "onNext: " + tiTypeList.get(1).getName());
                        for (int i = 0; i < tiTypeList.size(); i++) {
                            tiTypeListString.add(tiTypeList.get(i).getName());
                        }
                        fragments = new ArrayList<>();
                        for (int i = 0; i < tiTypeListString.size(); i++) {
                            Bundle bundle = new Bundle();
                            VpFragment vpFragment = new VpFragment();
                            List<TiTypeListBean.InfoBean.UTypeBean.ZTypeBean> z_type = tiTypeList.get(i).getZ_type();
                            ArrayList<String> strings = new ArrayList<>();
                            ArrayList<String> sizes = new ArrayList<>();
                            ArrayList<String> zuos = new ArrayList<>();
                            ArrayList<String> ti_ids = new ArrayList<>();
                            ArrayList<String> ban1s = new ArrayList<>();
                            for (int j = 0; j < z_type.size(); j++) {
                                strings.add(z_type.get(j).getName());
                                sizes.add(z_type.get(j).getZong());
                                zuos.add(z_type.get(j).getZuo());
                                ti_ids.add(z_type.get(j).getId());
                                ban1s.add(z_type.get(j).getBan_ben());
                            }
                            bundle.putStringArrayList("TypeList", strings);
                            bundle.putStringArrayList("sizes", sizes);
                            bundle.putStringArrayList("zuos", zuos);
                            bundle.putStringArrayList("ti1", ti_ids);
                            bundle.putStringArrayList("ban1", ban1s);

                            vpFragment.setArguments(bundle);
                            fragments.add(vpFragment);
                        }
//                        Log.e(TAG, "fragments.size" + tiTypeListString.size());
                        if (mVpNavMain != null && mTabMain != null) {
                            fragTabAdapter = new FragTabAdapter(getSupportFragmentManager(), fragments, tiTypeListString);
                            mVpNavMain.setAdapter(fragTabAdapter);
                            mTabMain.setupWithViewPager(mVpNavMain);
                        } else {
                            Toast.makeText(MainActivity.this, "tab为空", Toast.LENGTH_SHORT).show();
                        }

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e(TAG, "onError: " + e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        fragTabAdapter = new FragTabAdapter(getSupportFragmentManager(), fragments, tiTypeListString);
                        mVpNavMain.setAdapter(fragTabAdapter);
                        mTabMain.setupWithViewPager(mVpNavMain);
//                        fragTabAdapter.notifyDataSetChanged();
                    }
                });

    }

    @Override
    protected int createLayoutId() {
        return R.layout.activity_main;
    }

    private void initView() {
        mTabMain = findViewById(R.id.tab_main);
        mVpNavMain = findViewById(R.id.vp_nav_main);
        mVpNavMain.setAdapter(fragTabAdapter);
        mTabMain.setupWithViewPager(mVpNavMain);
        tiTypeListString = new ArrayList<>();
        initTiTypeList();
    }


    public static List<TiTypeListBean.InfoBean.UTypeBean> getTiTypeList() {
        return tiTypeList;
    }
}
